﻿
/// <reference path="_namespace.js" />
/// <reference path="../io/MouseButton.js"/>
/// <reference path="../components/Component.js" />
ajax.lang.addNamespace("ajax.events");
ajax.lang.imports("ajax.io.MouseButton");
ajax.lang.imports("ajax.components.Component");
//-----------------------------------------------------------------------------
ajax.events.EventInfo = function(eventObject)
{   /// <summary>Wraps a raw DOM Event into a cross browser Event info object</summary>
    /// <param name="eventObject">The raw DOM event</param>
    /// <field name="rawEvent" mayBeNull="true">The raw DOM event.</field>
    /// <field name="altKey" type="Boolean"></field>
    /// <field name="button" type="ajax.io.MouseButton"></field>
    /// <field name="charCode" type="Number" integer="true"></field>
    /// <field name="clientX" type="Number" integer="true"></field>
    /// <field name="clientY" type="Number" integer="true"></field>
    /// <field name="ctrlKey" type="Boolean"></field>
    /// <field name="keyCode" type="Number" integer="true"></field>
    /// <field name="offsetX" type="Number" integer="true"></field>
    /// <field name="offsetY" type="Number" integer="true"></field>
    /// <field name="screenX" type="Number" integer="true"></field>
    /// <field name="screenY" type="Number" integer="true"></field>
    /// <field name="shiftKey" type="Boolean"></field>
    /// <field name="target"></field>
    /// <field name="type" type="String"></field>        
    this.raw = eventObject || window.event || {target: null, srcElement: null };
    this.sender = this.raw.target || this.raw.srcElement;

    if (!eventObject)
        return;
        
    var e = eventObject;
    var etype = e.type ? e.type.toLowerCase() : "";
    this.rawEvent = e;
    this.altKey = e.altKey;
    if (typeof (e.button) !== 'undefined')
    {
        this.button = (typeof (e.which) !== 'undefined') ? e.button :
            (e.button === 4) ? ajax.io.MouseButton.MiddleButton :
            (e.button === 2) ? ajax.io.MouseButton.RightButton :
            ajax.io.MouseButton.LeftButton;            
    }
    if (etype === 'keypress')
    {
        this.charCode = e.charCode || e.keyCode;
    }
    else if (e.keyCode && (e.keyCode === 46))
    {
        this.keyCode = 127;
    }
    else
    {
        this.keyCode = e.keyCode;
    }
    this.clientX = e.clientX;
    this.clientY = e.clientY;
    this.ctrlKey = e.ctrlKey;
    this.target = e.target ? e.target : e.srcElement;
     
    //if (!etype.startsWith('key'))
    if (etype.substr(0, 3) != "key") 
    {
        if ((typeof (e.offsetX) !== 'undefined') && (typeof (e.offsetY) !== 'undefined'))
        {
            this.offsetX = e.offsetX;
            this.offsetY = e.offsetY;
        }
        else if (this.target && (this.target.nodeType !== 3) && (typeof (e.clientX) === 'number'))
        {
            var doc = ajax.components.Component.getElementDoc(this.target);
            var loc = ajax.components.Component.getElementPosition(doc, this.target, this.target.parentElement, true);
            var w = ajax.components.Component.getElementWindow(this.target);
            // MS code commented out...            
            //var loc = Sys.UI.DomElement.getLocation(this.target);
            //var w = Sys.UI.DomElement._getWindow(this.target);
            this.offsetX = (w.pageXOffset || 0) + e.clientX - loc.x;
            this.offsetY = (w.pageYOffset || 0) + e.clientY - loc.y;
        }
    }
    this.screenX = e.screenX;
    this.screenY = e.screenY;
    this.shiftKey = e.shiftKey;   
}

ajax.events.EventInfo.prototype = 
{
    raw: null,
    sender: null
}

//-----------------------------------------------------------------------------
ajax.events.EventInfo.prototype.toString = function()
{
    return "ajax.events.EventInfo";
}

//-----------------------------------------------------------------------------
ajax.events.EventInfo.cast = function(obj)
{///<returns type="ajax.events.EventInfo"/>
    return obj;    
}
